package com.dynomedia.esearch.util.groupkeycache.demo;

import com.dynomedia.esearch.util.groupkeycache.GroupkeyCache;
import com.dynomedia.esearch.util.groupkeycache.KeyBuffer;
import com.dynomedia.esearch.util.groupkeycache.Topkey;
import com.dynomedia.esearch.util.groupkeycache.util.GkcConfig;

public class Gkctest {

	public static void main(String[] args) {

		try {

			GkcConfig.getInstance().setGroupkeySortInterval(1);
			GkcConfig.getInstance().setStore(false);
			/**
			 * 仅当gkcfiles/persistent目录下存有较多缓存文件并且
			 * KeyBuffer写入压力较大时，需要调用GroupkeyCache.start
			 * 方法，提前完成GroupkeyCache的初始化。
			 */
			GroupkeyCache.start();

			// 组ID
			Long groupId = Long.parseLong("222");

			// 将key值添加至默认分组（Long.MAX_VALUE）
			KeyBuffer.add("key-1");
			KeyBuffer.add("key-1");
			KeyBuffer.add("key-1");
			KeyBuffer.add("key-2");
			KeyBuffer.add("key-2");

			// 将key值添加至标识为 222的分组内
			KeyBuffer.add(groupId, "key-1");
			KeyBuffer.add(groupId, "key-1");
			KeyBuffer.add(groupId, "key-1");
			KeyBuffer.add(groupId, "key-2");
			KeyBuffer.add(groupId, "key-2");

			/**
			 * 等待异步线程处理完毕
			 */
			Thread.sleep(2000);

			/**
			 * 获得默认组，及指定组（groupId）内标识为"key-1"的Key值对象
			 */
			System.out.println("\rkey-1 in defaultGroup:");
			System.out.println(GroupkeyCache.getTopkey("key-1"));

			System.out.println("\rkey-2 in Group 222:");
			System.out.println(GroupkeyCache.getTopkey(groupId, "key-2"));

			/**
			 * 获得默认组，及指定组（groupId）内的排序数组
			 */
			System.out.println("\rdefaultGroup:");
			for (Topkey key : GroupkeyCache.getTopkey()) 
				System.out.println(key);

			System.out.println("\rGroup 222:");
			for (Topkey key : GroupkeyCache.getTopkey(groupId)) 
				System.out.println(key);
			
			GroupkeyCache.shutdown();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
